package cn.imvc.app.models;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.imvc.app.domains.OrderState;
import cn.imvc.app.entities.GoodsOrder;
import cn.imvc.app.entities.Member;
import cn.imvc.app.entities.Orders;
import cn.imvc.app.entities.SalesReturn;
import cn.imvc.app.repositories.SalesReturnRepository;

@Service
public class SalesReturnModelImpl implements SalesReturnModel {

	@Autowired
	private SalesReturnRepository salesReturnRepository;

	@Autowired
	SettingModel settingModel;

	@Autowired
	GoodsOrderModel goodsOrderModel;

	@Autowired
	GoodsSkuModel goodsSkuModel;

	@Autowired
	GoodsModel goodsModel;

	@Override
	public SalesReturn save(SalesReturn salesReturn) {
		return salesReturnRepository.saveAndFlush(salesReturn);
	}

	@Override
	public Map<String, String> addSalesReturn(Member member, SalesReturn salesReturn, List<Integer> goodsOrderIds) {
		Map<String, String> result = new HashMap<String, String>();
		result.put("code", "0");
		if (member == null) {
			result.put("code", "-1");
			result.put("msg", "请登录");
			return result;
		}
		// 检测goodsOrderId串的订单号是否有效，即对应list的长度是否等于id个数
		// 对应的GoodsOrder的state是否是31，32，40，41四个值
		// 属不属于当前登陆用户订单
		String resultDetail = "";
		Orders order = new Orders();
		boolean check = false;
		GoodsOrder goodsOrder = new GoodsOrder();
		List<GoodsOrder> goodsOrderList = goodsOrderModel.findByGoodsOrderIdIn(goodsOrderIds);
		if (goodsOrderList != null && goodsOrderIds != null && goodsOrderList.size() == goodsOrderIds.size()) {
			for (int i = 0; i < goodsOrderList.size(); i++) {
				goodsOrder = goodsOrderList.get(i);
				if ((goodsOrder.getState() == OrderState.Delivered.getValue() || goodsOrder.getState() == OrderState.DeliveryFailed.getValue()
						|| goodsOrder.getState() == OrderState.Receipt.getValue()|| goodsOrder.getState() == OrderState.Delivery.getValue()
						|| goodsOrder.getState() == OrderState.SystemReceipt.getValue()) && goodsOrder.getMember().getId() == member.getId()) {
					check = true;
					resultDetail = resultDetail + goodsOrder.getId() + ":" + goodsOrder.getNum() + ",";
					if (i == 0) {
						order = goodsOrder.getOrder();
					}
				}
			}
		}
		if (check) {
			salesReturn.setReturnDetail(resultDetail.substring(0, resultDetail.length() - 1));
			salesReturn.setOrder(order);
			salesReturn.setOrderCode(order.getCode());
			salesReturn.setMember(member);
			salesReturn.setState((byte)0);
			salesReturn.setCreatedAt(new Date());
			save(salesReturn);
			for (int i = 0; i < goodsOrderList.size(); i++) {
				goodsOrder = goodsOrderList.get(i);
				goodsOrder.setState(OrderState.RefundApply.getValue());
				goodsOrder.setUpdatedAt(new Date());
				goodsOrderModel.save(goodsOrder);
			}
			result.put("code", "1");
			result.put("orderId", String.valueOf(order.getId()));
			result.put("msg", "申请成功");
		} else {
			result.put("code", "1");
			result.put("msg", "您没有权限操作");
		}
		return result;
	}

	public List<Object[]> findPriceByGoodsOrderIds(Member member, String goodsOrderIds) {
		String sql = "select sum(case when points <= 0 then 0 else o.totals end) from goods_order o where o.id in (" + goodsOrderIds + ") and o.memberId = " + member.getId();
		return salesReturnRepository.findAllSql(sql);
	}

}
